<?php
/**
 * Created by lzl
 * Date: 2021 2021/9/9
 * Time: 13:50
 */
namespace App\Http\Controllers\Admin;

use App\Exceptions\RequestException;
use App\Forms\GoodsAttributeForms;
use App\Forms\GoodsFileForms;
use App\Forms\GoodsForms;
use Illuminate\Validation\Rule;

class GoodsController extends Controller
{

    /**
     * 商品列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     * @throws \App\Exceptions\RequestException
     */
    public function goodsList()
    {
        if($this->request->ajax()) {
            $rules = [
                'page'       => 'required',
                'limit'      => 'required',
                'name'       => 'nullable'
            ];

            $this->validateInput($rules);

            return $this->showJsonLayui((new GoodsForms())->getGoodsList($this->validated));
        }

        return $this->view('goods.goods_list');
    }


    /**
     * 修改商品
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     * @throws RequestException
     * @throws \Throwable
     */
    public function editGoods()
    {
        $rules = [
            'id'                => 'nullable',
            'cat_id'            => 'nullable',
            'branch_id'         => 'nullable',
            'goods_name'        => 'nullable',
            'goods_description' => 'nullable',
            'images'            => 'nullable',
            'status'            => 'nullable',
            'is_new'            => 'nullable',
            'is_hot'            => 'nullable',
            'sort'              => 'nullable',
            'sales'             => 'nullable',
            'is_single'         => 'nullable',
            'sku'               => 'nullable',
            'sku_id'            => 'nullable',
            'spu'               => 'nullable',
        ];

        $this->validateInput($rules);

        $form = new GoodsForms();

        if($this->request->isMethod('post')) {

            $this->validateInput($rules);

            return $this->success($form->editGoods($this->validated));
        }

        return $this->view('goods.edit_goods', $form->getGoodsDetailForms($this->validated));
    }


    /**
     * 修改文件
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     * @throws RequestException
     * @throws \Throwable
     */
    public function editFiles($id)
    {
        $form = new GoodsFileForms();

        if($this->request->isMethod('post')) {

            $rules = [
                'banner_images'   => 'required|array', //主图
                'detail_images'   => 'required|array', //详情图
//                'param_images'    => 'required|array', //参数图片
                'download_video'  => 'nullable', //下载视频
                'download_images' => 'nullable', //下载图片
            ];

            $this->validateInput($rules);

            return $this->success($form->updateOrCreateFile(array_merge($this->validated, compact('id'))));
        }

        return $this->view('goods.edit_file', $form->getGoodsFile($id));
    }

    /**
     * 删除商品
     * @return mixed
     * @throws RequestException
     */
    public function deleteGoods()
    {
        $rules = [
            'id' => 'required|integer',
        ];

        $this->validateInput($rules);

        return $this->success((new GoodsForms())->deleteGoods($this->validated));
    }


    /**
     * 分类列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     * @throws \App\Exceptions\RequestException
     */
    public function categoryList()
    {
        if($this->request->ajax()) {
            $rules = [
                'page'       => 'required',
                'limit'      => 'required',
            ];

            $this->validateInput($rules);

            return $this->showJsonLayui((new GoodsForms())->getCategoryList($this->validated));
        }

        return $this->view('category.category_list');
    }


    /**
     * 修改分类
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     * @throws \App\Exceptions\RequestException
     */
    public function editCategory()
    {
        $rules = [
            'category_name' => 'nullable',
            'pid'           => 'nullable',
            'images'        => 'nullable',
            'id'            => 'nullable'
        ];

        $this->validateInput($rules);

        $form = new GoodsForms();

        if($this->request->isMethod('post')) {

            return $this->success($form->editCategory($this->validated));
        }

        return $this->view('category.edit_category',  $form->getCategoryOrNew($this->validated));
    }


    /**
     * 删除分类
     * @return mixed
     * @throws \App\Exceptions\RequestException
     */
    public function deleteCategory()
    {
        $rules = [
            'id' => 'required|integer',
        ];

        $this->validateInput($rules);

        return $this->success((new GoodsForms())->deleteCategory($this->validated));
    }


    /**
     * 品牌列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     * @throws \App\Exceptions\RequestException
     */
    public function branchList()
    {
        if($this->request->ajax()) {
            $rules = [
                'page'       => 'required',
                'limit'      => 'required',
            ];

            $this->validateInput($rules);

            return $this->showJsonLayui((new GoodsForms())->getBranchList($this->validated));
        }

        return $this->view('branch.branch_list');
    }


    /**
     * 修改品牌
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     * @throws \App\Exceptions\RequestException
     */
    public function editBranch()
    {
        $rules = [
            'images'      => 'nullable',
            'branch_name' => 'nullable',
            'id'          => 'nullable'
        ];

        $this->validateInput($rules);

        $form = new GoodsForms();

        if($this->request->isMethod('post')) {

            return $this->success($form->editBranch($this->validated));
        }

        return $this->view('branch.edit_branch', ['branch' => $form->getBranchOrNew($this->validated)]);
    }


    /**
     * 删除品牌
     * @return mixed
     * @throws \App\Exceptions\RequestException
     */
    public function deleteBranch()
    {
        $rules = [
            'id' => 'required|integer',
        ];

        $this->validateInput($rules);

        return $this->success((new GoodsForms())->deleteBranch($this->validated));
    }

    /**
     * @return mixed
     * @throws RequestException
     */
    public function getCategoryTree()
    {
        if($this->request->ajax()) {

            $rules = [
                'category_id' => 'required|integer'
            ];

            $this->validateInput($rules);

            return $this->success((new GoodsForms())->getCategoryTree($this->validated));
        }
        throw new RequestException('');
    }


    /**
     * 库存记录
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     * @throws RequestException
     */
    public function getStockLogList($id)
    {
        if($this->request->ajax()) {
            $rules = [
                'page'  => 'required',
                'limit' => 'required',
            ];

            $this->validateInput($rules);

            return $this->showJsonLayui((new GoodsForms())->getStockLogList(
                array_merge($this->validated, compact('id'))
            ));
        }

        return $this->view('goods.stock_log', compact('id'));
    }



    public function editGoodsAttribute($goods_id)
    {
        $rules = [
            'goods_id'          => 'nullable',
            'sku_id'            => 'nullable',
            'attribute_name'    => 'nullable',
            'stock'             => 'nullable',
            'price'             => 'nullable',
            'partner_price'     => 'nullable',
            'distributor_price' => 'nullable',
            'images'            => 'nullable',
        ];

        $this->validateInput($rules);

        $form = new GoodsAttributeForms();

        if($this->request->isMethod('post')) {

        }

    }

}